package com.book.app.controller;


import com.book.app.aop.SystemControllerLog;
import com.book.app.entity.Borrow;
import com.book.app.service.BorrowService;
import com.book.app.tools.CommonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;


@Api(tags = "图书借阅模块")
@RestController
@RequestMapping("borrow")
@CrossOrigin //前后端分离配置跨域访问
public class BorrowController  {
    /**
     * 服务对象
     */
    @Resource
    private BorrowService borrowService;


    @ApiOperation("分页查询借阅信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "分页起始数据",required = true,example = "1"),
            @ApiImplicitParam(name = "limit",value = "每页显示分页最大数据",required = true,example = "10"),
            @ApiImplicitParam(name = "bname",value = "借阅图书名称查询",example = "《红岩》"),
    })
    @SystemControllerLog(description ="分页查询借阅信息")
    @GetMapping("/findPager")
    public CommonResult findPager(Integer page, Integer limit, String bname){
        Map<String,Object>  map=new HashMap<>();
        map.put("page",page);//当前页码
        map.put("count",borrowService.counter(bname));//总条数
        map.put("data",borrowService.findPager(page,limit,bname));//数据集合
        return CommonResult.success(map);
    }

    @ApiOperation("添加借阅信息")
    @SystemControllerLog(description ="添加借阅信息")
    @PostMapping("/borrowBook")
    public  CommonResult borrowBook(@RequestBody Borrow borrow){
        boolean flag= borrowService.borrowBook(borrow);
        return flag?CommonResult.success(flag):CommonResult.failed();

    }

    @ApiOperation("归还图书")
    @SystemControllerLog(description ="归还图书")
    @PostMapping("/backBook")
    public  CommonResult backBook(@RequestBody Borrow borrow){
        boolean flag= borrowService.backBook(borrow);
        return flag?CommonResult.success(flag):CommonResult.failed();
    }

    @ApiOperation("统计热门借阅书籍")
    @GetMapping("/getBorrowCounter")
    public CommonResult getBorrowCounter() {
        return CommonResult.success(borrowService.getBorrowCounter());
    }
}

